/*
 * scraper.js
 * 
 * A collection of functions to scrape data from the google fusion table to display them to the table.
 *
 * AUTHORS
 *  STAN PECK
 *  PREBEN INGVALDSEN
 *  NATE KOZLOWSKI
 *
 *    
 */

var cs441ShibeDoge = cs441ShibeDoge || {};
cs441ShibeDoge.codeTranslator.createHashTables();

/*
 * setApplicantPop
 * 
 * Sets the total number of applicants in the given data from our FusionTable
 * 
 */
cs441ShibeDoge.setApplicantPop = function(data) {
	cs441ShibeDoge.dataObject.NUM_APPLICANTS = data.rows.length;
};

/*
 * setAverageGPA
 * 
 * Sets the average gpa in the given data from our Fusion Table
 * 
 */
cs441ShibeDoge.setAverageGPA = function(data) {
	num = data.rows.length;
	cs441ShibeDoge.dataObject.GPA = 0;
	for(var row = 0; row < data.rows.length; row++){
		if(!isNaN(data.rows[row][3])){
			cs441ShibeDoge.dataObject.GPA = cs441ShibeDoge.dataObject.GPA + parseInt(data.rows[row][3]);
		}
		else{
			num = num - 1;
		}
	}
	cs441ShibeDoge.dataObject.GPA = cs441ShibeDoge.dataObject.GPA / num;
	cs441ShibeDoge.dataObject.GPA = cs441ShibeDoge.dataObject.GPA.toFixed(2);
	
	if (cs441ShibeDoge.dataObject.GPA == "NaN") {
		cs441ShibeDoge.dataObject.GPA = "Unavailable";
	}
};

/*
 * setAverageVerbal
 * 
 * Sets the average verbal sat score of applicants in the given data from our Fusion Table
 * 
 */
cs441ShibeDoge.setAverageVerbal = function(data) {
	num = data.rows.length;
	cs441ShibeDoge.dataObject.SAT_VERBAL = 0;
	for(var row = 0; row < data.rows.length; row++){
		if(!isNaN(data.rows[row][4])){
			cs441ShibeDoge.dataObject.SAT_VERBAL = cs441ShibeDoge.dataObject.SAT_VERBAL + parseInt(data.rows[row][4]);
		}
		else{
			num = num - 1;
		}
	}
	cs441ShibeDoge.dataObject.SAT_VERBAL = cs441ShibeDoge.dataObject.SAT_VERBAL / num;
	cs441ShibeDoge.dataObject.SAT_VERBAL = cs441ShibeDoge.dataObject.SAT_VERBAL.toFixed(2);
	
	if (cs441ShibeDoge.dataObject.SAT_VERBAL == "NaN") {
		cs441ShibeDoge.dataObject.SAT_VERBAL = "Unavailable";
	}
};

/*
 * setAverageMath
 * 
 * Sets the average math sat score of applicants in the given data from our Fusion Table
 * 
 */
cs441ShibeDoge.setAverageMath = function(data) {
	num = data.rows.length;
	cs441ShibeDoge.dataObject.SAT_MATH = 0;
	for(var row = 0; row < data.rows.length; row++){
		if(!isNaN(data.rows[row][5])){
			cs441ShibeDoge.dataObject.SAT_MATH = cs441ShibeDoge.dataObject.SAT_MATH + parseInt(data.rows[row][5]);
		}
		else{
			num = num - 1;
		}
	}
	cs441ShibeDoge.dataObject.SAT_MATH = cs441ShibeDoge.dataObject.SAT_MATH / num;
	cs441ShibeDoge.dataObject.SAT_MATH = cs441ShibeDoge.dataObject.SAT_MATH.toFixed(2);
	if (cs441ShibeDoge.dataObject.SAT_MATH == "NaN") {
		cs441ShibeDoge.dataObject.SAT_MATH = "Unavailable";
	}
};

/*
 * setEnrolled
 * 
 * Sets the average in the given data from our Fusion Table
 * 
 */
cs441ShibeDoge.setEnrolled = function(data) {
	cs441ShibeDoge.dataObject.NUM_ENROLLED = 0;
	for(var row = 0; row < data.rows.length; row++){
		if(data.rows[row][11] == "Y"){
		cs441ShibeDoge.dataObject.NUM_ENROLLED = cs441ShibeDoge.dataObject.NUM_ENROLLED + 1;
		}
	}
	cs441ShibeDoge.dataObject.NUM_ENROLLED = cs441ShibeDoge.dataObject.NUM_ENROLLED;
};

/*
 * setAccepted
 * 
 * Sets the number of accepted applications
 * 
 */
cs441ShibeDoge.setAccepted = function(data) {
	cs441ShibeDoge.dataObject.NUM_ACCEPTED = 0;
	for(var row = 0; row < data.rows.length; row++){
		if(data.rows[row][10] == "CONF"){
		cs441ShibeDoge.dataObject.NUM_ACCEPTED = cs441ShibeDoge.dataObject.NUM_ACCEPTED + 1;
		}
	}
	cs441ShibeDoge.dataObject.NUM_ACCEPTED = cs441ShibeDoge.dataObject.NUM_ACCEPTED;
};

/*
 * setMostCommonReligion
 * 
 * Calculates and sets the most common religion for the given data from our FusionTable
 */
cs441ShibeDoge.setMostCommonReligion = function(data) {
	
	function isValidReligion(religionType) {
		return religionType != "NR" && religionType != undefined && religionType != "" && religionType != "NO"
			&& religionType != "UN";
	}
	//Create an array to hold our various religion types and an object
	//to be used as a hash for the religion counts
	var religionTypes = new Array();
	var religionCounts = {};
	var currentEntryType = "";
	for(var row = 0; row < data.rows.length; row++){
		currentEntryType = data.rows[row][26];

		//Set/increment the count for this type of religion and
		//add it to the types of religion if necessary
		if (religionCounts[currentEntryType] == undefined) {
			religionCounts[currentEntryType] = 1;
			religionTypes.push(currentEntryType);
		}
		else {
			religionCounts[currentEntryType]++;
		}
	}

	//Calculate the most commonly occurring religion
	var mostCommonCount = 0;
	var mostCommonReligion = "";
	for (var i = 0; i < religionTypes.length; i++) {
		currentEntryType = religionTypes[i];
		if (religionCounts[currentEntryType] > mostCommonCount && isValidReligion(currentEntryType)) {
			mostCommonCount = religionCounts[currentEntryType];
			mostCommonReligion = currentEntryType;
		}
	}
	cs441ShibeDoge.dataObject.RELIGION = mostCommonReligion;
	if (cs441ShibeDoge.dataObject.RELIGION == "") {
		cs441ShibeDoge.dataObject.RELIGION = "Unavailable";
	}
};

/*
 * setMostMajor
 * 
 * Calculates and sets the most common major for the given data from our FusionTable
 */
cs441ShibeDoge.setMostCommonMajor = function(data) {
	
	function isValidMajor(majorCode) {
		return !isNaN(majorCode);
	}
	//Create an array to hold our various religion types and an object
	//to be used as a hash for the religion counts
	var majorTypes = new Array();
	var majorCounts = {};
	var currentEntryType = "";
	for(var row = 0; row < data.rows.length; row++){
		currentEntryType = data.rows[row][22];

		//Set/increment the count for this type of major and
		//add it to the types of major if necessary
		if (majorCounts[currentEntryType] == undefined) {
			majorCounts[currentEntryType] = 1;
			majorTypes.push(currentEntryType)
		}
		else {
			majorCounts[currentEntryType]++;
		}
	}

	//Calculate the most commonly occurring major
	var mostCommonCount = 0;
	var mostCommonMajor = "";
	for (var i = 0; i < majorTypes.length; i++) {
		currentEntryType = majorTypes[i];
		if (majorCounts[currentEntryType] > mostCommonCount && isValidMajor(currentEntryType)) {
			mostCommonCount = majorCounts[currentEntryType];
			mostCommonMajor = currentEntryType;
		}
	}
	
	cs441ShibeDoge.dataObject.MAJOR = mostCommonMajor;
	
	if (cs441ShibeDoge.dataObject.MAJOR == "") {
		cs441ShibeDoge.dataObject.MAJOR = "Unavailable";
	}
};

/*
 * setMostGender
 * 
 * Calculates and sets the most common gender for the given data from our FusionTable
 */
cs441ShibeDoge.setMostCommonGender = function(data) {
	
	function isValidGender(genderCode) {
		if(genderCode == "F" || genderCode == "M"){
			return true;
		}
		else{
			return false;
		}
	}
	//Create an array to hold our various religion types and an object
	//to be used as a hash for the religion counts
	var genderTypes = new Array();
	var genderCounts = {};
	var currentEntryType = "";
	for(var row = 0; row < data.rows.length; row++){
		currentEntryType = data.rows[row][20];

		//Set/increment the count for this type of major and
		//add it to the types of major if necessary
		if (genderCounts[currentEntryType] == undefined) {
			genderCounts[currentEntryType] = 1;
			genderTypes.push(currentEntryType)
		}
		else {
			genderCounts[currentEntryType]++;
		}
	}

	//Calculate the most commonly occurring gender
	var mostCommonCount = 0;
	var mostCommonGender = "";
	for (var i = 0; i < genderTypes.length; i++) {
		currentEntryType = genderTypes[i];
		if (genderCounts[currentEntryType] > mostCommonCount && isValidGender(currentEntryType)) {
			mostCommonCount = genderCounts[currentEntryType];
			mostCommonGender = currentEntryType;
		}
	}

	cs441ShibeDoge.dataObject.GENDER = mostCommonGender;
};

/*
 * setMostState
 * 
 * Calculates and sets the most common state for the given data from our FusionTable
 */
cs441ShibeDoge.setMostCommonState = function(data) {
	
	function isValidState(stateCode) {
		if(stateCode == ""){
			return false;
		}
		else{
			return true;
		}
	}
	//Create an array to hold our various religion types and an object
	//to be used as a hash for the state counts
	var stateTypes = new Array();
	var stateCounts = {};
	var currentEntryType = "";
	for(var row = 0; row < data.rows.length; row++){
		currentEntryType = data.rows[row][15];

		//Set/increment the count for this type of state and
		//add it to the types of major if necessary
		if (stateCounts[currentEntryType] == undefined) {
			stateCounts[currentEntryType] = 1;
			stateTypes.push(currentEntryType)
		}
		else {
			stateCounts[currentEntryType]++;
		}
	}

	//Calculate the most commonly occurring state
	var mostCommonCount = 0;
	var mostCommonState = "";
	for (var i = 0; i < stateTypes.length; i++) {
		currentEntryType = stateTypes[i];
		if (stateCounts[currentEntryType] > mostCommonCount && isValidState(currentEntryType)) {
			mostCommonCount = stateCounts[currentEntryType];
			mostCommonState = currentEntryType;
		}
	}

	cs441ShibeDoge.dataObject.MOST_POPULAR_STATE = mostCommonState;
};

/*
 * setMostCity
 * 
 * Calculates and sets the most common city for the given data from our FusionTable
 */
cs441ShibeDoge.setMostCommonCity = function(data) {
	
	function isValidCity(cityCode) {
		if(cityCode == ""){
			return false;
		}
		else{
			return true;
		}
	}
	//Create an array to hold our various religion types and an object
	//to be used as a hash for the state counts
	var cityTypes = new Array();
	var cityCounts = {};
	var currentEntryType = "";
	for(var row = 0; row < data.rows.length; row++){
		currentEntryType = data.rows[row][14];

		//Set/increment the count for this type of state and
		//add it to the types of major if necessary
		if (cityCounts[currentEntryType] == undefined) {
			cityCounts[currentEntryType] = 1;
			cityTypes.push(currentEntryType)
		}
		else {
			cityCounts[currentEntryType]++;
		}
	}

	//Calculate the most commonly occurring state
	var mostCommonCount = 0;
	var mostCommonCity = "";
	for (var i = 0; i < cityTypes.length; i++) {
		currentEntryType = cityTypes[i];
		if (cityCounts[currentEntryType] > mostCommonCount && isValidCity(currentEntryType)) {
			mostCommonCount = cityCounts[currentEntryType];
			mostCommonCity = currentEntryType;
		}
	}

	cs441ShibeDoge.dataObject.MOST_POPULAR_CITY = mostCommonCity;
};

/*
 * setMostRace
 * 
 * Calculates and sets the most common ethnicity for the given data from our FusionTable
 * in the most racially sensitive way possible.
 */
cs441ShibeDoge.setMostCommonEthnicity = function(data) {
	
	function isValidEthnicity(ethnicCode) {
		if(isNaN(ethnicCode)){
			return false;
		}
		else{
			return true;
		}
	}
	//Create an array to hold our various religion types and an object
	//to be used as a hash for the state counts
	var ethnicTypes = new Array();
	var ethnicCounts = {};
	var currentEntryType = "";
	for(var row = 0; row < data.rows.length; row++){
		currentEntryType = data.rows[row][18];

		//Set/increment the count for this type of state and
		//add it to the types of major if necessary
		if (ethnicCounts[currentEntryType] == undefined) {
			ethnicCounts[currentEntryType] = 1;
			ethnicTypes.push(currentEntryType)
		}
		else {
			ethnicCounts[currentEntryType]++;
		}
	}

	//Calculate the most commonly occurring state
	var mostCommonCount = 0;
	var mostCommonEthnicity = "";
	for (var i = 0; i < ethnicTypes.length; i++) {
		currentEntryType = ethnicTypes[i];
		if (ethnicCounts[currentEntryType] > mostCommonCount && isValidEthnicity(currentEntryType)) {
			mostCommonCount = ethnicCounts[currentEntryType];
			mostCommonEthnicity = currentEntryType;
		}
	}

	cs441ShibeDoge.dataObject.ETHNICITY = mostCommonEthnicity;
	if (cs441ShibeDoge.dataObject.ETHNICITY == "") {
		cs441ShibeDoge.dataObject.ETHNICITY = "Unavailable";
	}
};

/*
 * scrapeData
 * 
 * A function that pulls all entries from the Admissions Data FusionTable, then parses it into the proper format
 * for our Data Table
 * 
 */
cs441ShibeDoge.scrapeData = function() {
	request = new XMLHttpRequest();

	url = "https://www.googleapis.com/fusiontables/v1/query?sql=SELECT * FROM 13na5H4_enS7_zftNnAhsd1JWpgDBVv6tg5P_624&key=";
	url = url + "AIzaSyDkeXsopORSm1LWznQIgrUn0Z2h768EWqs";

	request.onreadystatechange = cs441ShibeDoge.parseData;
	request.open('GET', url);
	request.send();
};

/*
 * parseData
 * 
 * A function that parses data taken from the Admissions Data FusionTable, then places that
 * data into our Data Table
 * 
 */
cs441ShibeDoge.parseData = function(data) {
	//Make sure everything went according to plan
	if (request.readyState === 4){
		if(request.status === 200){
			//Translate our response into something we can mess with
			data = JSON.parse(request.responseText);
			//Save the data for later use
			cs441ShibeDoge.data = data;
			cs441ShibeDoge.allApplicantRows = data.rows.slice(0);
			cs441ShibeDoge.currentDataSet = data;
			cs441ShibeDoge.calculateDataFields(data);
			cs441ShibeDoge.populateStateSelectDropDown(data);
			cs441ShibeDoge.populateGenderSelectDropDown(data);
			cs441ShibeDoge.populateMajorSelectDropDown(data);
			cs441ShibeDoge.populateEthnicitySelectDropDown(data);
			var regionSelect = document.getElementById("regionSelect");
			regionSelect.style.display = 'block';
		}
	}
};

/*
 * updateValues
 * 
 * A function that changes reference codes to legible text
 * 
 */
cs441ShibeDoge.updateValues = function() {
	//populating the hashtables
	var toCheck;
	var newVal;
	
	//Updating Gender
	var toCheck = cs441ShibeDoge.dataObject.GENDER;
	if (toCheck === "F") {
		cs441ShibeDoge.dataObject.GENDER = "Female";
	} else {
		cs441ShibeDoge.dataObject.GENDER = "Male";
	}
	
	//updating religion
	toCheck = cs441ShibeDoge.dataObject.RELIGION;
	if (toCheck != 'Unavailable') {
		newVal = cs441ShibeDoge.codeTranslator.religions[toCheck];
		cs441ShibeDoge.dataObject.RELIGION = newVal;
	}
	
	//updating ethnicity
	toCheck = cs441ShibeDoge.dataObject.ETHNICITY;
	if (toCheck != 'Unavailable') {
		newVal = cs441ShibeDoge.codeTranslator.ethnicity[toCheck];
		cs441ShibeDoge.dataObject.ETHNICITY = newVal;
	}
	
	//updating major
	toCheck = cs441ShibeDoge.dataObject.MAJOR;
	if (toCheck != 'Unavailable') {
		newVal = cs441ShibeDoge.codeTranslator.majors[toCheck];
		cs441ShibeDoge.dataObject.MAJOR = newVal;
	}
};


/*
 * calculateDataFields
 * 
 * A function that calculates the data fields for the various entries in our summary data
 * table and sets them in the data object in the namespace cs441ShibeDoge
 */
cs441ShibeDoge.calculateDataFields = function(data) {
	cs441ShibeDoge.setApplicantPop(data);
	cs441ShibeDoge.setAverageGPA(data);
	cs441ShibeDoge.setMostCommonReligion(data);
	cs441ShibeDoge.setAverageVerbal(data);
	cs441ShibeDoge.setAverageMath(data);
	cs441ShibeDoge.setEnrolled(data);
	cs441ShibeDoge.setAccepted(data);
	cs441ShibeDoge.setMostCommonMajor(data);
	cs441ShibeDoge.setMostCommonGender(data);
	cs441ShibeDoge.setMostCommonState(data);
	cs441ShibeDoge.setMostCommonCity(data);
	cs441ShibeDoge.setMostCommonEthnicity(data);
	cs441ShibeDoge.updateValues();
	cs441ShibeDoge.showTables();
	cs441ShibeDoge.populateTable(cs441ShibeDoge.dataObject);
};

/*
 * scrapeHighSchoolNames
 * 
 * Scrapes the names of high schools corresponding to the selected state
 * 
 * PARAMS
 * 	state - The currently selected state
 */
cs441ShibeDoge.scrapeHighSchoolNames = function(state) {	
	request = new XMLHttpRequest();

	url = "https://www.googleapis.com/fusiontables/v1/query?sql=SELECT * FROM 1SInAG971GTVrZOVqt0ORpMDPWYAb5DjoWpPCnRg WHERE State='" + state + "'&key=";
	url = url + "AIzaSyDkeXsopORSm1LWznQIgrUn0Z2h768EWqs";

	request.onreadystatechange = cs441ShibeDoge.parseHighSchools;
	request.open('GET', url);
	request.send();
}
